﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XDesigner.ORM;

namespace XDesigner.ORM.Test
{
    public partial class frmReadCustomerTree : Form
    {
        public frmReadCustomerTree()
        {
            InitializeComponent();
        }

        private void cmdRefreshCustomer_Click(object sender, EventArgs e)
        {
            tvwCustomer.BeginUpdate();
            this.Cursor = Cursors.WaitCursor;

            tvwCustomer.Nodes.Clear();
            CustomerClass[] curs = (CustomerClass[])Program.ORM.ReadAllInstances(typeof(CustomerClass));
            foreach (CustomerClass cur in curs)
            {
                TreeNode customerNode = new TreeNode(cur.CompanyName);
                customerNode.Tag = cur;
                customerNode.ImageIndex = 0;
                customerNode.SelectedImageIndex = 0;
                tvwCustomer.Nodes.Add(customerNode);

                TreeNode tempNode = new TreeNode("Loading...");
                customerNode.Nodes.Add(tempNode);

            }//foreach

            tvwCustomer.EndUpdate();

            this.Cursor = Cursors.Default;
        }


        private void btnRefreshEmployee_Click(object sender, EventArgs e)
        {
            tvwCustomer.BeginUpdate();
            this.Cursor = Cursors.WaitCursor;

            tvwCustomer.Nodes.Clear();
            EmployeeClass[] employees = (EmployeeClass[])Program.ORM.ReadAllInstances(typeof(EmployeeClass));
            foreach (EmployeeClass employee in employees)
            {
                TreeNode employeeNode = new TreeNode(employee.FirstName + employee.LastName);
                employeeNode.Tag = employee;
                employeeNode.ImageIndex = 0;
                employeeNode.SelectedImageIndex = 0;
                tvwCustomer.Nodes.Add(employeeNode);
                foreach (OrderClass order in employee.Orders)
                {
                    TreeNode orderNode = new TreeNode(order.ShipName);
                    orderNode.Tag = order;
                    orderNode.ImageIndex = 1;
                    orderNode.SelectedImageIndex = 1;
                    employeeNode.Nodes.Add(orderNode);
                    foreach (OrderDetailClass detail in order.OrderDetails)
                    {
                        TreeNode detailNode = new TreeNode(detail.Product.ProductName);
                        detailNode.Tag = detail;
                        detailNode.ImageIndex = 2;
                        detailNode.SelectedImageIndex = 2;
                        orderNode.Nodes.Add(detailNode);

                    }//foreach
                }//foreach
            }//foreach

            tvwCustomer.EndUpdate();

            this.Cursor = Cursors.Default;
        }

        private void tvwCustomer_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvwCustomer.SelectedNode != null)
            {
                myPropertyGrid.SelectedObject = tvwCustomer.SelectedNode.Tag;
            }
        }

        private void btnSaveORMDom_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.CheckPathExists = true;
                dlg.OverwritePrompt = true;
                dlg.Filter = "XML file|*.xml";
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    System.Xml.Serialization.XmlSerializer ser 
                        = new System.Xml.Serialization.XmlSerializer(typeof(XDesigner.ORM.DOM.ORMProjectInfo));
                    using (System.IO.FileStream file = 
                        new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                    {
                        ser.Serialize(file, Program.ORM.Project );
                    }
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (tvwCustomer.SelectedNode != null)
            {
                object obj = tvwCustomer.SelectedNode.Tag;
                int result = Program.ORM.Delete(obj);
                if (result > 0)
                {
                    tvwCustomer.SelectedNode.Remove();
                }
                MessageBox.Show("Delete " + result + " records");
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tvwCustomer.SelectedNode != null)
            {
                object obj = tvwCustomer.SelectedNode.Tag;
                int result = Program.ORM.Update(obj);
                MessageBox.Show("Update " + result + " records");
            }
        }

        private void tvwCustomer_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text == "Loading..." )
            {
                e.Node.Nodes.Clear();
                System.Collections.IEnumerable orders = null;
                if (e.Node.Tag is CustomerClass)
                {
                    CustomerClass cur = (CustomerClass)e.Node.Tag;
                    orders = ((CustomerClass)e.Node.Tag).Orders;
                }
                else if( e.Node.Tag is EmployeeClass )
                {
                    orders = (( EmployeeClass ) e.Node.Tag ).Orders ;
                }
                if( orders != null )
                {
                    foreach (OrderClass order in orders)
                    {
                        TreeNode orderNode = new TreeNode(order.ShipName);
                        orderNode.Tag = order;
                        orderNode.ImageIndex = 1;
                        orderNode.SelectedImageIndex = 1;
                        e.Node.Nodes.Add(orderNode);
                        foreach (OrderDetailClass detail in order.OrderDetails)
                        {
                            TreeNode detailNode = new TreeNode(detail.Product.ProductName);
                            detailNode.Tag = detail;
                            detailNode.ImageIndex = 2;
                            detailNode.SelectedImageIndex = 2;
                            orderNode.Nodes.Add(detailNode);

                        }//foreach
                    }//foreach
                }
            }
        }
    }
}